﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIProgressBarTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetReadOnlyProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIProgressBar progressBar = window.UIProgressBar("@AutomationId='progressBar1'");
            Assert.IsTrue(progressBar.IsRangeReadOnly);
        }

        [TestMethod]
        public void ShouldGetMinimumValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIProgressBar progressBar = window.UIProgressBar("@AutomationId='progressBar1'");
            Assert.AreEqual(0.0, progressBar.Minimum);
        }

        [TestMethod]
        public void ShouldGetMaximumValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIProgressBar progressBar = window.UIProgressBar("@AutomationId='progressBar1'");
            Assert.AreEqual(100.0, progressBar.Maximum);
        }

        [TestMethod]
        public void ShouldGetCurrentValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIProgressBar progressBar = window.UIProgressBar("@AutomationId='progressBar1'");
            Assert.AreEqual(50.0, progressBar.RangeValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ShowThrowArgumentExceptionForInvalidTimeoutOnWaitForCompletion()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIProgressBar progressBar = window.UIProgressBar("@AutomationId='progressBar1'");
            progressBar.WaitForCompletion(-1);
        }

        [TestMethod]
        public void ShowReturnFalseWhenTimeoutReachedOnWaitForCompletion()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIProgressBar progressBar = window.UIProgressBar("@AutomationId='progressBar1'");
            bool isCompleted = progressBar.WaitForCompletion(100);
            Assert.IsFalse(isCompleted);
        }

        [TestMethod]
        public void ShouldReturnTrueOnWaitForCompletion()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIProgressBar progressBar = window.UIProgressBar("@AutomationId='progressBar2'");
            bool isCompleted = progressBar.WaitForCompletion(100);
            Assert.IsTrue(isCompleted);
        }

        [TestMethod]
        public void ShouldGetAllProgressBars()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIProgressBar> progressBars = window.UIProgressBars();
            Assert.AreEqual(2, progressBars.Count);
            Assert.AreEqual("progressBar1", progressBars[0].AutomationId);
            Assert.AreEqual("progressBar2", progressBars[1].AutomationId);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}